﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;
using CqusInfo.RAL.Permissions;
using CqusInfo.CR.SqlHelper;

namespace CqusInfo.DAL.Permissions
{
    public class User_FunctionDAL:Inteface.IUser_FunctionDAL
    {
        public Int32 GetCounts(bool isDel)
        {
            return SQLiteHelper.GetCounts("tb_User_Function", isDel);
        }
        /// <summary>
        /// 得到参数
        /// </summary>
        /// <param name="model">数据集</param>
        /// <returns>parameters</returns>
        private SQLiteParameter[] GetParameters(string id, User_FunctionModel model)
        {
            SQLiteParameter[] parameters ={
                                         new SQLiteParameter("@ufId",DbType.String),
                                         new SQLiteParameter("@userName",DbType.String),
                                         new SQLiteParameter("@fId",DbType.String)
                                         };
            parameters[0].Value = id.Trim() == null ? Guid.NewGuid().ToString() : id;
            parameters[1].Value = model.c_vc_userName;
            parameters[2].Value = model.c_vc_fId;

            return parameters;
        }

        public Int32 Add(RAL.Permissions.User_FunctionModel model)
        {
            StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append("insert into tb_User_Function values(");
            sqlStr.Append("c_vc_ufId=@ufId,c_vc_userName=@userName,c_vc_fId=@fId)");

            return SQLiteHelper.ExecuteNonQuery(sqlStr.ToString(), GetParameters(null, model));
        }

        public Int32 Update(string ufId, RAL.Permissions.User_FunctionModel model)
        {
            StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append("update tb_User_Function set ");
            sqlStr.Append("c_vc_userName=@userName,c_vc_fId=@fId where c_vc_ufId=@ufId");

            return SQLiteHelper.ExecuteNonQuery(sqlStr.ToString(), GetParameters(ufId, model));
        }

        public Int32 Delete(string ufId)
        {
            return SQLiteHelper.Delete("tb_User_Function", "c_vc_ufId", ufId);
        }

        public List<User_FunctionModel> Query(string ufId)
        {
            List<User_FunctionModel> roleList = new List<User_FunctionModel>();
            DataSet ds = SQLiteHelper.Query("tb_User_Function", "c_vc_rfId", ufId);
            string cacheId = string.Format("User_FunctionModel_{0}", ufId);
            roleList = SqlData.GetQueryList<User_FunctionModel>(cacheId, "User_FunctionModel", ds);
            return roleList;
        }

        public List<User_FunctionModel> Query(Int32 page, Int32 takeItem)
        {
            List<User_FunctionModel> roleList = new List<User_FunctionModel>();
            DataSet ds = SQLiteHelper.Query("tb_User_Function", page, takeItem);
            string cacheId = string.Format("User_FunctionModel_{0}_{1}", page, takeItem);
            roleList = SqlData.GetQueryList<User_FunctionModel>(cacheId, "User_FunctionModel", ds);
            return roleList;
        }
    }
}
